let BaseParser = require('./BaseParser');
let CatalogItemParser = require('./CatalogItemParser');
/**
 * 各类资源解析器
 * 
 * @class CatalogsParser
 * @extends {BaseParser}
 */
class CatalogsParser extends BaseParser {
    constructor() {
        super('CatalogsParser');
    }
    parser(body) {
        let $ = this.cheerio.load(body, { decodeEntities: false });
        let data = [];
        $('.g-container .subject').each((index, elem) => {
            let $elem = $(elem);
            data.push(this.toModal($elem, $));
        });
        return data;
        // console.log(data);
    }
    toModal(elem, $) {
        let subItems = [];
        let removeEnterAndSpace = this.removeEnterAndSpace;
        elem.find('.subject-items li').each((index, item) => {
            let $item = $(item);
            let catalogItemParser = new CatalogItemParser();
            subItems.push(catalogItemParser.parser($item, $));
        })
        return {
            subject_title: removeEnterAndSpace(elem.find('h3').text()),
            subject_items: subItems
        }
    }
}

module.exports = CatalogsParser;